package sort;

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

public class SortArrayDistanceK {
    public static void main(String[] args) {
       int[] arr = {10, 5, 3, 9, 2};
       sortArrayDistanceK(arr,3);
        System.out.println(Arrays.toString(arr));
    }
    public static void sortArrayDistanceK(int[] arr, int k) {
        PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
        int index = 0;
        for(;index <= Math.min(k,arr.length);index++){
            pq.add(arr[index]);
        }
        int i = 0;
        for(i = 0;index<arr.length;i++,index++){
            pq.add(arr[index]);
            arr[i] = pq.poll();
        }
        while(!pq.isEmpty()){
            arr[i++] = pq.poll();
        }
    }
}
